﻿using System;
using System.Text.RegularExpressions;

namespace Twitter
{
    public class MessageTextNormalizer
    {
        private static Regex whitespaceMatcher = new Regex("\\s+", RegexOptions.Compiled);

        public void NormalizeWhitespaceAndCasing(Message message)
        {
            if (message == null) throw new ArgumentNullException("message");
            if (String.IsNullOrEmpty(message.Text))
            {
                message.Text = string.Empty;
            }
            else
            {
                string originalMessageText = message.Text;
                string messageTextWithNormalizedWhitespace = whitespaceMatcher.Replace(originalMessageText, " ");

                string trimmedMessageText = messageTextWithNormalizedWhitespace.Trim();
                string messageTextWithoutCapitals = trimmedMessageText.ToLower();

                message.Text = messageTextWithoutCapitals;
            }

        }
    }
}
